package com.qys.livesMall.framework.view;


import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serial;
import java.io.Serializable;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @menu BaseQuery
 * @date 2023/05/19 17:30
 */
@Data
public class BaseQuery implements Serializable {

    @Serial
    private static final long serialVersionUID = 42L;

    public int total = 0;

    /**
     * 包含count查询
     */
    private boolean count = true;

    /**
     * 页面大小
     */
    @Schema(description = "每页显示大小", example = "20")
    private Integer pageSize = 10;

    /**
     * 页码，从1开始
     */
    @Schema(description = "页码", example = "1")
    private Integer pageNum = 1;

    @Schema(description = "开始时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;

    @Schema(description = "结束时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    @Schema(description = "id", example = "123")
    private String id;

    @Schema(description = "name", example = "123")
    private String name;

    @Schema(description = "排序方式", example = "DESC / ASC")
    private String sort;

    public void setStartTimeLong(Long startTime) {
        if (null == startTime){
            return;
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault());
        this.startTime = localDateTime;
    }

    public void setEndTimeLong(Long endTime) {
        if (null == endTime){
            return;
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault());
        this.endTime = localDateTime;
    }

    public void setStartTimeLocal(LocalDateTime startTime) {
        if (null == startTime){
            return;
        }
        this.startTime=startTime;
    }

    public void setEndTimeLocal(LocalDateTime endTime) {
        if (null == endTime){
            return;
        }
        this.endTime = endTime;
    }

    public void setStartTime(Object startTime) {
        if (null == startTime){
            return;
        }
        if (startTime instanceof LocalDateTime) {
           setStartTimeLocal((LocalDateTime) startTime);
        }else if(startTime instanceof Long){
            setStartTimeLong((Long) startTime);
        }

    }

    public void setEndTime(Object endTime) {
        if (endTime instanceof LocalDateTime) {
            setEndTimeLocal((LocalDateTime) endTime);
        }else if(endTime instanceof Long){
            setEndTimeLong((Long) endTime);
        }
    }


}

